package com.iring.dao.vos;

import org.springframework.stereotype.Component;

import com.iring.domain.vos.BaseBack;
import com.iring.domain.vos.customer.Customer;
import com.iring.domain.vos.phone.DeletePhone;
import com.iring.domain.vos.phone.ModifyPhone;
import com.iring.domain.vos.phone.VosPhone;
import com.iring.domain.vos.suite.CreateSuiteOrder;
import com.iring.domain.vos.suite.Suite;
import com.iring.exception.ParamInvalidException;

/**
 * 通用返回类
 *
 * @author wanggan
 */
@Component
public class CommonVosDao extends BaseVosDao<BaseBack> {

    private static final String CREATE_CUSTOMER = "CreateCustomer";
    private static final String DELETE_SUITE_ORDER = "DeleteSuiteOrder";
    private static final String CREATE_SUITE_ORDER = "CreateSuiteOrder";
    private static final String DELETE_PHONE = "DeletePhone";
    private static final String CREATE_PHONE = "CreatePhone";
    private static final String MODIFY_PHONE = "ModifyPhone";

    /**
     * 基础转换方法
     * 
     * @param jsonStr
     *            json字符串
     * @return {@link BaseBack}
     */
    public Boolean convertToBaseBack(String jsonStr) {
        BaseBack baseBack = convertToObject(jsonStr, BaseBack.class);
        Integer retCode = baseBack.getRetCode();
        if(null == retCode){
            throw new ParamInvalidException(jsonStr);
        }
        if(retCode == 0.0){
            return Boolean.TRUE;
        }
        return Boolean.FALSE;

    }

    /**
     * 创建vos账户
     * 
     * @param customer
     *            创建vos用户信息的参数
     * @return   返回baseback对象
     */
    public BaseBack createVosCustomer(Customer customer)  {
        return convertToObject(util.postData(CREATE_CUSTOMER, customer), BaseBack.class);
    }

    /**
     * 创建订单套餐
     *
     * @param suiteOrder
     *            创建套餐对象的参数
     * @return 创建成功<code>true</code>
     * 创建失败抛出异常
     * 其他异常<code>false</code>
     */
    public Boolean createVosSuiteOrder(CreateSuiteOrder suiteOrder) {
        return convertToBaseBack(util.postData(CREATE_SUITE_ORDER, suiteOrder));
    }

    /**
     * 删除订单套餐
     *
     * @param id
     *            订单套餐id
     * @return 创建成功<code>true</code>
     * 创建失败抛出异常
     * 其他异常<code>false</code>
     */
    public Boolean deleteOrderSuite(int id) {
        return convertToBaseBack(util.postData(DELETE_SUITE_ORDER, new Suite(id)));
    }

    /**
     * 创建vos话机
     *
     * @param vosPhone
     *            创建话机的请求参数
     * @return 创建成功<code>true</code>
     * 创建失败抛出异常
     * 其他异常<code>false</code>
     */
    public Boolean createVosPhone(VosPhone vosPhone) {
        return convertToBaseBack(util.postData(CREATE_PHONE, vosPhone));
    }

    /**
     * 修改通话级别,例如升级为国际长途等等
     *
     * @param modifyPhone
     *            修改话机的参数
     * @return 修改成功<code>true</code>
     * 创建失败抛出异常
     * 其他异常<code>false</code>
     */
    public Boolean modifyPhoneCallLevel(ModifyPhone modifyPhone) {
        return convertToBaseBack(util.postData(MODIFY_PHONE, modifyPhone));
    }

    /**
     * 删除话机
     *
     * @param e164s
     *            话机号码
     * @return 删除话机返回值
     */
    public Boolean deletePhone(String e164s) {
        return convertToBaseBack(util.postData(DELETE_PHONE, new DeletePhone(e164s)));
    }

}
